import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout/Layout.vue'
import AdminInterviewList from '@/views/admin/InterviewList.vue'
import AdminApplicationList from '@/views/admin/ApplicationList.vue'
import Home from '@/views/Home.vue'
import Login from '@/views/Login.vue'
import RegisterStudent from '@/views/RegisterStudent.vue'
import RegisterEnterprise from '@/views/RegisterEnterprise.vue'
import StudentHome from '@/views/StudentHome.vue'
import EnterpriseHome from '@/views/EnterpriseHome.vue'
import AdminHome from '@/views/AdminHome.vue'
import Profile from '@/views/profile/Profile.vue'
import PositionDetail from '@/views/student/PositionDetail.vue'
import PositionList from '@/views/student/PositionList.vue'
import Resume from '@/views/student/Resume.vue'
import ApplicationList from '@/views/student/ApplicationList.vue'
import InterviewList from '@/views/student/InterviewList.vue'
import EnterpriseInfo from '@/views/enterprise/Info.vue'
import EnterprisePositionList from '@/views/enterprise/PositionList.vue'
import EnterprisePositionEdit from '@/views/enterprise/PositionEdit.vue'
import EnterpriseApplicationList from '@/views/enterprise/ApplicationList.vue'
import EnterpriseInterviewList from '@/views/enterprise/InterviewList.vue'
import EnterpriseInviteStudent from '@/views/enterprise/InviteStudent.vue'
import AdminAuditUser from '@/views/admin/AuditUser.vue'
import NotFound from '@/views/NotFound.vue'
import NoPermission from '@/views/NoPermission.vue'
import StudentInfo from '@/views/student/Info.vue'
import AdminStudents from '@/views/admin/Students.vue'
import AdminEnterprises from '@/views/admin/Enterprises.vue'
import AdminInfo from '@/views/admin/AdminInfo.vue'

const routes = [
  { path: '/login', name: 'Login', component: Login },
  { path: '/register/student', name: 'RegisterStudent', component: RegisterStudent },
  { path: '/register/enterprise', name: 'RegisterEnterprise', component: RegisterEnterprise },
  {
    path: '/',
    component: Layout,
    children: [
      { path: '', name: 'Home', component: Home },
      { path: 'student', name: 'StudentHome', component: StudentHome },
      { path: 'student/info', name: 'StudentInfo', component: StudentInfo },
      { path: 'student/positions', name: 'PositionList', component: PositionList },
      { path: 'student/resume', name: 'Resume', component: Resume },
      { path: 'student/applications', name: 'ApplicationList', component: ApplicationList },
      { path: 'student/interviews', name: 'InterviewList', component: InterviewList },
      { path: 'student/position/:id', name: 'PositionDetail', component: PositionDetail },
      { path: 'student/enterprises', name: 'EnterpriseList', component: () => import('@/views/student/Enterprises.vue') },
      { path: 'enterprise', name: 'EnterpriseHome', component: EnterpriseHome },
      { path: 'enterprise/info', name: 'EnterpriseInfo', component: EnterpriseInfo },
      { path: 'enterprise/positions', name: 'EnterprisePositionList', component: EnterprisePositionList },
      { path: 'enterprise/position/publish', name: 'EnterprisePositionPublish', component: EnterprisePositionEdit },
      { path: 'enterprise/position/:id', name: 'EnterprisePositionEdit', component: EnterprisePositionEdit },
      { path: 'enterprise/applications', name: 'EnterpriseApplicationList', component: EnterpriseApplicationList },
      { path: 'enterprise/interviews', name: 'EnterpriseInterviewList', component: EnterpriseInterviewList },
      { path: 'enterprise/invite', name: 'EnterpriseInviteStudent', component: EnterpriseInviteStudent },
      { path: 'admin', name: 'AdminHome', component: AdminHome },
      { path: 'admin/applications', name: 'AdminApplicationList', component: AdminApplicationList },
      { path: 'admin/interviews', name: 'AdminInterviewList', component: AdminInterviewList },
      { path: 'admin/audit', name: 'AdminAuditUser', component: AdminAuditUser },
      { path: 'admin/students', name: 'AdminStudents', component: AdminStudents },
      { path: 'admin/enterprises', name: 'AdminEnterprises', component: AdminEnterprises },
      { path: 'admin/info', name: 'AdminInfo', component: AdminInfo },
      { path: 'profile', name: 'Profile', component: Profile },
      { path: '403', name: 'NoPermission', component: NoPermission },
      { path: ':pathMatch(.*)*', name: 'NotFound', component: NotFound },

    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  // 登录和注册页面全部放行
  if (to.path === '/login' || to.path.startsWith('/register')) {
    next()
    return
  }
  const token = localStorage.getItem('token')
  const userStore = (await import('@/store/user')).useUserStore()
  if (!token) {
    next('/login')
  } else if (token) {
    // token存在但userInfo丢失，自动拉取
    if (!userStore.userInfo || !userStore.role) {
      try {
        await userStore.fetchUserInfo()
        if (!userStore.userInfo || !userStore.role) {
          userStore.logout()
          next('/login')
          return
        }
      } catch (e) {
        userStore.logout()
        next('/login')
        return
      }
    }
    next()
  } else {
    next()
  }
})

export default router 